﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgingTest.Tools;

namespace AgingTest.Tools
{
    public class SendCheckData
    {
        
        public byte[] SendData = new byte[15];  //存放最终要发送的数据

        public byte[] DataType = new byte[1];   //包类型
        public byte[] DataLength = new byte[2]; //数据长度
        public byte[] CANID = new byte[2];      //CANId
        public byte[] CommCode = new byte[1];   //指令代码
        public byte[] MotoId = new byte[4];     //电机ID
        public byte[] CRCCode = new byte[4];      //4位CRC校验
        public byte[] EndCode = new byte[1];    //结束码

        /// <summary>
        /// 组装 开始测试 数据结构
        /// </summary>
        /// <param name="str_CANID"></param>
        public void AssembleData_Begin(string str_CANID)
        {
            CommCode = new byte[] { 0x02 };     //指令代码为 开始测试

            DataType = new byte[] { 0x0A };
            DataLength = new byte[] { 0x0F, 0x00 };
            CANID = CANToHex(str_CANID); 
            MotoId = new byte[] { 0x01, 0x00, 0x00, 0x00 };
            EndCode[0] = 0xFF;

            byte[] temp = new byte[10];
            Array.Copy(DataType, 0, temp, 0, 1);
            Array.Copy(DataLength, 0, temp, 1, 2);
            Array.Copy(CANID, 0, temp, 3, 2);
            Array.Copy(CommCode, 0, temp, 5, 1);
            Array.Copy(MotoId, 0, temp, 6, 4);
            CRCCode = CRC32.Calculate_CRC32_Byte(temp);

            Array.Copy(DataType, 0, SendData, 0, 1);
            Array.Copy(DataLength, 0, SendData, 1, 2);
            Array.Copy(CANID, 0, SendData, 3, 2);
            Array.Copy(CommCode, 0, SendData, 5, 1);
            Array.Copy(MotoId, 0, SendData, 6, 4);
            Array.Copy(CRCCode, 0, SendData, 10, 4);
            Array.Copy(EndCode, 0, SendData, 14, 1);

            

        }

        //组装轮询
        public void AssembleData_Polling(string str_CANID)
        {
            CommCode = new byte[] { 0x00 };     //指令代码为 轮询

            DataType = new byte[] { 0x0A };
            DataLength = new byte[] { 0x0F, 0x00 };
            CANID = CANToHex(str_CANID);
            MotoId = new byte[] { 0x01, 0x00, 0x00, 0x00 };
            EndCode[0] = 0xFF;

            byte[] temp = new byte[10];
            Array.Copy(DataType, 0, temp, 0, 1);
            Array.Copy(DataLength, 0, temp, 1, 2);
            Array.Copy(CANID, 0, temp, 3, 2);
            Array.Copy(CommCode, 0, temp, 5, 1);
            Array.Copy(MotoId, 0, temp, 6, 4);
            CRCCode = CRC32.Calculate_CRC32_Byte(temp);

            Array.Copy(DataType, 0, SendData, 0, 1);
            Array.Copy(DataLength, 0, SendData, 1, 2);
            Array.Copy(CANID, 0, SendData, 3, 2);
            Array.Copy(CommCode, 0, SendData, 5, 1);
            Array.Copy(MotoId, 0, SendData, 6, 4);
            Array.Copy(CRCCode, 0, SendData, 10, 4);
            Array.Copy(EndCode, 0, SendData, 14, 1);
        }

        //组装停止
        public void AssembleData_Stop(string str_CANID)
        {
            CommCode = new byte[] { 0x04 };     //指令代码为 停止

            DataType = new byte[] { 0x0A };
            DataLength = new byte[] { 0x0F, 0x00 };
            CANID = CANToHex(str_CANID);
            MotoId = new byte[] { 0x01, 0x00, 0x00, 0x00 };
            EndCode[0] = 0xFF;

            byte[] temp = new byte[10];
            Array.Copy(DataType, 0, temp, 0, 1);
            Array.Copy(DataLength, 0, temp, 1, 2);
            Array.Copy(CANID, 0, temp, 3, 2);
            Array.Copy(CommCode, 0, temp, 5, 1);
            Array.Copy(MotoId, 0, temp, 6, 4);
            CRCCode = CRC32.Calculate_CRC32_Byte(temp);

            Array.Copy(DataType, 0, SendData, 0, 1);
            Array.Copy(DataLength, 0, SendData, 1, 2);
            Array.Copy(CANID, 0, SendData, 3, 2);
            Array.Copy(CommCode, 0, SendData, 5, 1);
            Array.Copy(MotoId, 0, SendData, 6, 4);
            Array.Copy(CRCCode, 0, SendData, 10, 4);
            Array.Copy(EndCode, 0, SendData, 14, 1);
        }



        private byte[] HexStringToString(string hs)
        {
            string strTemp = "";
            byte[] b = new byte[hs.Length / 2];
            for (int i = 0; i < hs.Length / 2; i++)
            {
                strTemp = hs.Substring(i * 2, 2);
                b[i] = Convert.ToByte(strTemp, 16);
            }
            //按照指定编码将字节数组变为字符串
            return b;
        }

        private byte[] CANToHex(string CAN_ID)
        {
            byte[] CAN_ID_Hex = HexStringToString(CAN_ID);
            byte tempByte = CAN_ID_Hex[0];
            CAN_ID_Hex[0] = CAN_ID_Hex[1];
            CAN_ID_Hex[1] = tempByte;
            return CAN_ID_Hex;
        }


    }

    

}
